package com.online.onlineshop.services.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.online.onlineshop.entity.Production;
import com.online.onlineshop.entity.User;
import com.online.onlineshop.mapper.ProductionMapper;
import com.online.onlineshop.mapper.UserMapper;
import com.online.onlineshop.services.ForProductionService;
import com.online.onlineshop.utils.ReturnCode;
import com.online.onlineshop.utils.ReturnData;
import com.online.onlineshop.vo.CommentVo;
import com.online.onlineshop.vo.SalesVolumeParamVo;
import com.online.onlineshop.vo.SalesVolumeVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ForProductionServiceImpl implements ForProductionService {

    @Autowired
    private ProductionMapper productionMapper;

    @Autowired
    private UserMapper userMapper;


    @Override
    public ReturnData<List<Production>> listAll(int pageNum, int limit) {
        Page<Production> page = new Page<>(pageNum, limit);
        Page<Production> pg = productionMapper.selectPage(page, null);
        return new ReturnData<>(pg.getRecords(), pg.getTotal(), pg.getCurrent());
    }

    @Override
    public ReturnData<List<Production>> selectByName(String name, int pageNum, int limit) {
        Page<Production> page = new Page<>(pageNum, limit);
        LambdaQueryWrapper<Production> wrapper = new LambdaQueryWrapper<>();
        if (!StringUtils.isBlank(name)) {
            wrapper.like(Production::getTitle, name);
        }
        Page<Production> pg = productionMapper.selectPage(page, wrapper);
        return new ReturnData<>(pg.getRecords(), pg.getTotal(), pg.getCurrent());
    }

    @Override
    public ReturnData<List<Production>> selectByProduction(Production production, int pageNum, int limit, int uid) {
        Page<Production> page = new Page<>(pageNum, limit);
        LambdaQueryWrapper<Production> wrapper = new LambdaQueryWrapper<>();
        User user = userMapper.selectById(uid);
        if (user.getUserType() != 2) {
            wrapper.eq(Production::getUid, uid);
        }
        if (production.getId() != null) {
            wrapper.eq(Production::getId, production.getId());
        }
        if (!StringUtils.isBlank(production.getTitle())) {
            wrapper.like(Production::getTitle, production.getTitle());
        }
        Page<Production> pg = productionMapper.selectPage(page, wrapper);
        return new ReturnData<>(pg.getRecords(), pg.getTotal(), pg.getCurrent());
    }

    @Override
    public ReturnData<String> addProduction(int uid, String userName, Production production) {
        production.setUid(uid);
        production.setCreatedUser(userName);
        production.setCreatedTime(new Date());
        production.setModifiedUser(userName);
        production.setModifiedTime(new Date());
        production.setPriority(0);
        production.setStatus(0);
        int rs = productionMapper.insert(production);
        if (rs != 1) {
            throw new RuntimeException("添加商品异常");
        }
        return new ReturnData<String>(ReturnCode.SUCCESS);
    }

    @Override
    public ReturnData<String> updateByPid(String userName, Production production) {
        production.setModifiedUser(userName);
        production.setModifiedTime(new Date());
        int rs = productionMapper.updateById(production);
        if (rs != 1) {
            throw new RuntimeException("更新商品异常");
        }
        return new ReturnData<String>(ReturnCode.SUCCESS);
    }

    //根据销量排序
    @Override
    public List<Production> selectHotList() {
        LambdaQueryWrapper<Production> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(Production::getPriority).last("limit 4");

        return productionMapper.selectList(wrapper);
    }

    //根据添加时间排序
    @Override
    public List<Production> selectNewList() {
        LambdaQueryWrapper<Production> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(Production::getCreatedTime).last("limit 4");

        return productionMapper.selectList(wrapper);
    }


    /*
     *
     * 商品详细页方法
     * */
    @Override
    public Production selectByPid(int pid) {
        Production production = productionMapper.selectById(pid);
        if (production == null) {
            throw new RuntimeException("商品不存在");
        }
        return production;
    }

    @Override
    public ReturnData<String> deleteByPid(int pid) {
        int rs = productionMapper.deleteById(pid);
        if (rs != 1) {
            throw new RuntimeException("删除商品时出现未知错误");
        }
        return new ReturnData<String>(ReturnCode.SUCCESS);
    }

    @Override
    public ReturnData<List<Production>> selectByProduct(Production production, int pageNum, int limit) {
        Page<Production> page = new Page<>(pageNum, limit);
        LambdaQueryWrapper<Production> wrapper = new LambdaQueryWrapper<>();
        if (!StringUtils.isBlank(production.getTitle())) {
            wrapper.like(Production::getTitle, production.getTitle());
        }if (!StringUtils.isBlank(production.getItemType())) {
            wrapper.eq(Production::getItemType, production.getItemType());
        }
        Page<Production> pg = productionMapper.selectPage(page, wrapper);
        return new ReturnData<>(pg.getRecords(), pg.getTotal(), pg.getCurrent());
    }

    @Override
    public ReturnData<List<SalesVolumeVo>> selectSalesVolumeById(SalesVolumeParamVo vo, int uid, int pageNum, int limit) {
        Page<SalesVolumeVo> page = new Page<>(pageNum, limit);
        SalesVolumeParamVo paramVo = new SalesVolumeParamVo();
        paramVo.setUid(uid);
        paramVo.setPid(vo.getPid());
        IPage<SalesVolumeVo> pg = productionMapper.selectSalesVolumeById(page, paramVo);
        return new ReturnData<>(pg.getRecords(), pg.getTotal(), pg.getCurrent());
    }
}


